In [68]:
!cat index.yaml


indexes: 
  - kind: Art
    properties: 
    - name: year
    - name: artist
    - name: Title

In [65]:
!gcloud datastore --help


NAME
    gcloud datastore - manage your Cloud Datastore indexes

SYNOPSIS
    gcloud datastore COMMAND [GCLOUD_WIDE_FLAG ...]

DESCRIPTION
    The gcloud datastore command group lets you create and delete Google Cloud
    Datastore indexes.

    Cloud Datastore is a highly-scalable NoSQL database for your applications.
    Cloud Datastore automatically handles sharding and replication, providing
    you with a highly available and durable database that scales automatically
    to handle your applications' load.

    More information on Cloud Datastore can be found here:
    https://cloud.google.com/datastore and detailed documentation can be found
    here: https://cloud.google.com/datastore/docs

GCLOUD WIDE FLAGS
    These flags are available to all commands: --account, --configuration,
    --flatten, --format, --help, --log-http, --project, --quiet, --trace-token,
    --user-output-enabled, --verbosity. Run $ gcloud help for details.

COMMANDS
    COMMAND is one of the following:

     cleanup-indexes
        Remove unused datastore indexes based on your local index
        configuration.

     create-indexes
        Create new datastore indexes based on your local index configuration.

EXAMPLES
    To create new indexes from a file, run:

        $ gcloud datastore create-indexes index.yaml

    To clean up unused indexes from a file, run:

        $ gcloud datastore cleanup-indexes index.yaml

In [66]:
!gcloud datastore create-indexes --help


NAME
    gcloud datastore create-indexes - create new datastore indexes based on
        your local index configuration

SYNOPSIS
    gcloud datastore create-indexes INDEX_FILE [GCLOUD_WIDE_FLAG ...]

DESCRIPTION
    This command creates new datastore indexes based on your local index
    configuration. Any indexes in your index file that do not exist will be
    created.

POSITIONAL ARGUMENTS
     INDEX_FILE
        The path to your index.yaml file.

GCLOUD WIDE FLAGS
    These flags are available to all commands: --account, --configuration,
    --flatten, --format, --help, --log-http, --project, --quiet, --trace-token,
    --user-output-enabled, --verbosity. Run $ gcloud help for details.

EXAMPLES
    To create new indexes based on your local configuration, run:

        $ gcloud datastore create-indexes ~/myapp/index.yaml

    Detailed information about index configuration can be found at the
    index.yaml reference
    (https://cloud.google.com/appengine/docs/standard/python/config/indexref).

In [69]:
!gcloud datastore create-indexes index.yaml --quiet


Configurations to update:

descriptor:      [index.yaml]
type:            [datastore indexes]
target project:  [future-sonar-168815]



In [70]:
!gcloud datastore cleanup-indexes index.yaml --quiet


Configurations to update:

descriptor:      [index.yaml]
type:            [datastore indexes]
target project:  [future-sonar-168815]



In [ ]:


In [1]:
# import
from google.cloud import datastore

First you have to authenticate the google python library with

gcloud beta auth application-default login


In [ ]:
#client initialization
client=datastore.Client(project='future-sonar-168815')

In [2]:
#let's create an new entity
key=client.key('Art')
entity=datastore.Entity(key,exclude_from_indexes=['Title'])
print(entity)

In [19]:
#add some values
entity.update({'Title':'Sunrise Beach at Sunset',
    'artist':'Pat Sumner',
    'year':2012})
print(entity)


<Entity('Art', 5760820306771968) {'Title': 'Sunrise Beach at Sunset', 'artist': 'Pat Sumner', 'year': 2012}>

In [20]:
#put the new entity in the cloud datastore
client.put(entity)

In [23]:
#another one
key=client.key('Art')
entity=datastore.Entity(key,exclude_from_indexes=['Title'])
entity.update({'Title':'Laura near the Crossroads',
               'artist':'K. Kim Jones',
                'year':2015})
client.put(entity)

In [22]:
#another one
key=client.key('Art')
entity=datastore.Entity(key,exclude_from_indexes=['Title'])
entity.update({'Title':'Login / Logout',
               'artist':'Pat Sumner',
                'year':2008})
client.put(entity)

In [24]:
#let's make a query and have a look at the result
query=client.query(kind='Art',filters=[('artist','=','Pat Sumner')])
for result in query.fetch():
    print(result)


<Entity('Art', 5715999101812736) {'year': 2008, 'artist': 'Pat Sumner', 'Title': 'Login / Logout'}>
<Entity('Art', 5760820306771968) {'year': 2012, 'artist': 'Pat Sumner', 'Title': 'Sunrise Beach at Sunset'}>

In [63]:
# Let's delete them
query=client.query(kind='Art',filters=[('artist','=','Pat Sumner')])
client.delete_multi([result.key for result in query.fetch()])

In [82]:
#check if the deletes have been done correctly 
query=client.query(kind='Art')
for result in query.fetch():
    print(result)


<Entity('Art', 5096363633147904) {'year': 2015, 'artist': 'K. Kim Jones', 'Title': 'Laura near the Crossroads'}>
<Entity('Art', 5644406560391168) {'year': 2015, 'artist': 'K. Kim Jones', 'Title': 'Laura near the Crossroads'}>
<Entity('Art', 5720147234914304) {'year': 2015, 'artist': 'K. Kim Jones', 'Title': 'Laura near the Crossroads'}>

In [ ]: